<?PHP
class TWError {

	public static $debug = array();

	public $no, $type, $file, $line, $message;

	public function __construct($errno, $errstr, $errfile, $errline) {
		$error = false;

		// Switch over the error type and report whether theshould be considered.
		switch($errno) {
		case E_USER_ERROR:        if(TeeWee::$CONFIG->REPORT_ERROR)
			$this->type="User Error";             												break;
		case E_USER_WARNING:      if(TeeWee::$CONFIG->REPORT_WARNING)
			$this->type="User Warning";           												break;
		case E_USER_NOTICE:       if(TeeWee::$CONFIG->REPORT_NOTICE)
			$this->type="User Notice";            												break;
		case E_ERROR:             $this->type="Error";                  break;
		case E_WARNING:           $this->type="Warning";                break;
		case E_PARSE:             $this->type="Parse Error";            break;
		case E_NOTICE:            $this->type="Notice";                 break;
		case E_CORE_ERROR:        $this->type="Core Error";             break;
		case E_CORE_WARNING:      $this->type="Core Warning";           break;
		case E_COMPILE_ERROR:     $this->type="Compile Error";          break;
		case E_COMPILE_WARNING:   $this->type="Compile Warning";        break;
		case E_STRICT:            $this->type="Strict Notice";          break;
		case E_RECOVERABLE_ERROR: $this->type="Recoverable Error";      break;
		default:                  $this->type="Unknown Error";					break;
		}

		// If the TeeWee admin doesn't care about certain types of
		// warnings, the errorcode is not executed.
		if(!is_null($this->type)) {
			$this->no				= $errno;
			$this->message	= $errstr;
			$this->file			= substr($errfile,strlen(TeeWee::$DIR));
			$this->line			= $errline;
		}

	}

	public function report() {
		if(!is_null($this->type)) {
			if(TeeWee::$CONFIG->REPORT_MAIL_ON) {
				$to =
					TeeWee::$CONFIG->TEEWEE_OWNER_NAME.
					'<'.TeeWee::$CONFIG->TEEWEE_OWNER_MAIL.'>';
				
				$subject = preg_replace(
					'/\{site\}/',
					TeeWee::$CONFIG->TEEWEE_NAME,
					TeeWee::$CONFIG->REPORT_SUBJECT);

				ob_start();
				print_r($_REQUEST);
				$request = nl2br(ob_get_clean());
				$data = array(
					'TeeWee'		=> TeeWee::$CONFIG->TEEWEE_NAME,
					'URL'				=> 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'],
					'Page'			=> TeeWee::$PAGE->path,
					'Timestamp' => date('Y-m-d H:i:s'),
					'Request'		=> $request,
				);
				$message = '<html><body>';
				foreach($data as $k => $v)
					$message .= "<b>$k</b> : $v<br />";
				$message .=	$this->as_html();
				$message .= '</body></html>';
				
				$nl = TeeWee::$CONFIG->MAIL_HEADER_NLCR ? "\n" : "\r\n";
				$headers	= 'From: TeeWee@'.$_SERVER['SERVER_NAME'].$nl;
				$headers .= 'MIME-Version: 1.0'.$nl;
				$headers .= 'Content-type: text/html; charset=iso-8859-1'.$nl;

				mail($to,$subject,$message,$headers);
			}
			if(TeeWee::$CONFIG->TEEWEE_DEBUG) {
				// skip the generated output so far
				ob_end_clean();
				// print the given debug lines
				if(sizeof(self::$debug) > 0)
					echo "DEBUG LINES<br />";
				foreach(self::$debug as $msg)
					echo "<p>$msg</p>";
				// print the error report as HTML
				echo $this->as_html();
				exit();
			}
			else {
				self::internal_error();
			}
		}
	}

	public function as_html() {
		return
			'<pre>'.
			"<b><u>TEEWEE DEBUG REPORT</u></b>\n".
			"<b>Type</b> : {$this->type} ({$this->no})\n".
			"<b>File</b> : {$this->file}\n".
			"<b>Line</b> : {$this->line}\n".
			"<b>Message</b>\n{$this->message}".
			'</pre>';
	}

	public function __toString() {
		return
			"[{$this->type}({$this->no})] ".
			"in {$this->file} on line {$this->line} : ".
			"{$this->message}\n";
	}

	public static function error_handler($errno, $errstr, $errfile, $errline) {
			$error = new TWError($errno, $errstr, $errfile, $errline);
			$error->report();
	}

	public static function shutdown() {
		if(is_null($error = error_get_last()) === false) {
			self::error_handler(
				$error['type'],
				$error['message'],
				$error['file'],
				$error['line']
			);
		}
		exit();
	}

	public static function internal_error() {
		TeeWee::$TEEWEE->redirect_intern(TeeWee::$PAGE,null);
	}

}
?>
